package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by karkarrotxu on 2016/10/24.
 */
public class ApiSnapshotUtil {
    private static Logger logger = LoggerFactory.getLogger(ApiSnapshotUtil.class);
    public static final String SNAPSHOT_START_URL = Configuration.getString("snapshot.start.url");
    public static final String SNAPSHOT_STOP_URL = Configuration.getString("snapshot.stop.url");
    public static final String SNAPSHOT_GAP_SECOND = Configuration.getString("snapshot.gap.second");
    public static final String SNAPSHOT_CALLBACK_URL = Configuration.getString("snapshot.callback.url");

    public static final int CODE_SUC = 1;
    public static final int CODE_ERROR_FATAL = -999;
    public static final int CODE_FAIL = -1;
    public static final int CODE_ERROR_ENCODE_URL = -2;
    public static final int CODE_ERROR_TASK_ID = -3;
    public static final int CODE_ERROR_REQUEST_FAIL = -4;
    public static final int CODE_ERROR_DATA = -5;

    public static final String STR_CODE_SUC = Constants.CODE_SUC;
    public static final String STR_CODE_ERROR_FATAL = "ERROR_FATAL";
    public static final String STR_CODE_FAIL = "FAIL";
    public static final String STR_CODE_ERROR_ENCODE_URL = "ERROR_ENCODE_URL";
    public static final String STR_CODE_ERROR_TASK_ID = "ERROR_TASK_ID";
    public static final String STR_CODE_ERROR_REQUEST_FAIL = "ERROR_REQUEST_FAIL";
    public static final String STR_CODE_ERROR_DATA = "ERROR_DATA";
    public static final String STR_CODE_NO_DATA = "NO_DATA";

    private ApiSnapshotUtil() {
    }

    public static String doSnapStart(long taskid, String rtmpurl, int gapTime) {
        if (taskid < 1) {
            return STR_CODE_ERROR_TASK_ID;
        }
        String encodeUrl = null;
        try {
            encodeUrl = Base64.encode(rtmpurl.getBytes("UTF-8"));
        } catch (Exception e) {
            logger.error("[ApiSnapshotUtil][doStart][error][taskid_rtmpurl:" + taskid + "_" + rtmpurl, e);
            return STR_CODE_ERROR_ENCODE_URL;
        }

        Map<String, String> params = new HashMap<>();
        params.put("taskid", taskid + "");
        params.put("rtmpurl", encodeUrl);
        params.put("interval", gapTime < 0 ? SNAPSHOT_GAP_SECOND : gapTime + "");
        params.put("callback", SNAPSHOT_CALLBACK_URL);
        String retValue = ApiHttpRequestUtil.doGet(SNAPSHOT_START_URL, null, params);
        if (retValue != null) {
            try {
                JSONObject jsonObject = JSONObject.parseObject(retValue);
                if (Constants.CODE_SUC.equals(jsonObject.getString("code"))) {
                    return STR_CODE_SUC;
                } else {
                    return retValue;
                }
            } catch (Exception e) {
                return STR_CODE_ERROR_DATA;
            }
        }
        return STR_CODE_ERROR_REQUEST_FAIL;
    }

    public static String doSnapStop(long taskId) {
        if (taskId < 1) {
            return STR_CODE_ERROR_TASK_ID;
        }
        Map<String, String> params = new HashMap<>();
        params.put("taskid", taskId + "");
        String retValue = ApiHttpRequestUtil.doGet(SNAPSHOT_STOP_URL, null, params);
        if (retValue != null) {
            try {
                JSONObject jsonObject = JSONObject.parseObject(retValue);
                if (Constants.CODE_SUC.equals(jsonObject.getString("code"))) {
                    return STR_CODE_SUC;
                } else {
                    return retValue;
                }
            } catch (Exception e) {
                return STR_CODE_ERROR_DATA;
            }
        }
        return STR_CODE_ERROR_REQUEST_FAIL;
    }
}
